Ecological data analysis with R

news
code
analysis
Author

Hafez Ahmad

Published

October 16, 2022

Data analysis with R.

# Load necessary libraries
library(ggplot2)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
# Create a sample ecological dataset
data <- data.frame(
  Species = rep(c("Species A", "Species B", "Species C"), each = 10),
  Abundance = c(rpois(10, 5), rpois(10, 10), rpois(10, 15)),
  Location = rep(c("Site 1", "Site 2"), each = 15)
)

# Summarize data by species
summary_data <- data %>%
  group_by(Species) %>%
  summarize(Mean_Abundance = mean(Abundance), .groups = 'drop')

# Print summary
print(summary_data)
# A tibble: 3 × 2
  Species   Mean_Abundance
  <chr>              <dbl>
1 Species A            5.1
2 Species B           10.3
3 Species C           15.2
# Plot species abundance by location
ggplot(data, aes(x = Species, y = Abundance, fill = Location)) +
  geom_boxplot() +
  labs(title = "Species Abundance by Location",
       x = "Species",
       y = "Abundance") +
  theme_minimal()